package com.wskeee.ec.components.skin
{
	import flash.display.Bitmap;
	import flash.display.BitmapData;
	import flash.display.LineScaleMode;
	import flash.display.Sprite;
	import flash.events.Event;
	import flash.events.ProgressEvent;
	import flash.geom.Matrix;
	
	import com.wskeee.ec.components.Image;
	import com.wskeee.ec.containers.LayoutConst;
	import com.wskeee.ec.core.EComponent;

	public class ImageSkin extends BaseSkinDrawer
	{
		//--------------------------------------------------------------------------
		//
		//  varables
		//
		//--------------------------------------------------------------------------
		private var content:Bitmap;
		private var loading:Progresser;
		private var contentData:BitmapData;
		
		private var reflshContentFlag:Boolean = false;
		private var refixContentFlag:Boolean = false;
		private var redrawBackgroupFlag:Boolean = false;
		
		//--------------------------------------------------------------------------
		//
		//  Properties
		//
		//--------------------------------------------------------------------------
		private function get img():Image
		{
			return target as Image;
		}
		private function get clip():Sprite
		{
			return ui as Sprite;
		}
		
		//--------------------------------------------------------------------------
		//
		//  Method
		//
		//--------------------------------------------------------------------------
		override public function init(target:EComponent, ui:*):void
		{
			super.init(target,ui);
			content = new Bitmap();
			clip.addChild(content);
			initTargetEvent();
			initProgresser();
		}
		
		//-----------------------------------------------------------------
		//  validateProperties
		//-----------------------------------------------------------------
		override public function validateProperties():void
		{
			if(reflshContentFlag && img.sourceWidth)
			{
				var sx:Number = img.width/img.sourceWidth;
				var sy:Number = img.height/img.sourceHeight;
				var scale:Number = sx>sy ? sy : sx;
				var m:Matrix = new Matrix(scale,0,0,scale);
				contentData = new BitmapData(img.sourceWidth*scale,img.sourceHeight*scale,true,0);
				contentData.draw(img.bitmapData,m,null,null,null,true);
				content.bitmapData = contentData;
				reflshContentFlag = false;
			}
		}
		
		override public function validateDisplayList():void
		{
			if(refixContentFlag)
			{
				var tx:Number;
				var ty:Number;
				if(!img.scale)
				{
					switch(img.horizontalAlign)
					{
						case LayoutConst.LEFT:
							tx = 0;
							break;	
						case LayoutConst.CENTER:
							tx = (img.width - content.width)/2;
							break;
						case LayoutConst.RIGHT:
							tx = img.width - content.width;
							break;
					}
					switch(img.verticalAlign)
					{
						case LayoutConst.TOP:
							ty = 0;
							break;	
						case LayoutConst.MIDDLE:
							ty = (img.height - content.height)/2;
							break;
						case LayoutConst.BOTTOM:
							ty = img.height - content.height;
							break;
					}
				}else
				{
					tx = (img.width - content.width)/2;
					ty = (img.height - content.height)/2;
				}
				
				content.x = tx;
				content.y = ty;
				refixContentFlag = false;
			}
			if(redrawBackgroupFlag)
			{
				drawBackgrounp();
				redrawBackgroupFlag = false;
			}
		}
		
		//-----------------------------------------------------------------
		//  destory
		//-----------------------------------------------------------------
		override public function destory():void
		{
			target.removeEventListener(Event.COMPLETE,onCompleteHander);
			target.removeEventListener(ProgressEvent.PROGRESS,onProgressHandler);
			target.removeEventListener(Event.RESIZE,onResize);
			contentData = null;
			content = null;
			super.destory();
		}
		
		//-----------------------------------------------------------------
		//  progresser
		//-----------------------------------------------------------------
		private function initProgresser():void
		{
			loading = new Progresser();
		}
		
		//-----------------------------------------------------------------
		//  target event
		//-----------------------------------------------------------------
		private function initTargetEvent():void
		{
			target.addEventListener(Event.COMPLETE,onCompleteHander);
			target.addEventListener(ProgressEvent.PROGRESS,onProgressHandler);
			target.addEventListener(Event.RESIZE,onResize);
		}
		
		private function onResize(event:Event):void
		{
			if(img.scale)
				reflshContentFlag = true;
			refixContentFlag = true;
			redrawBackgroupFlag = true;
		}
		
		private function onProgressHandler(event:ProgressEvent):void
		{
			if(img.enableLoadingState && img.bytesLoaded!=img.bytesTotal)
			{
				clip.addChild(loading);
				loading.value = Math.round(img.bytesLoaded/img.bytesTotal*100);
			}else
			{
				if(clip.contains(loading))
					clip.removeChild(loading);
			}
		}
		
		private function onCompleteHander(event:Event):void
		{
			if(!img.scale)
				content.bitmapData = img.bitmapData;
			onResize(null);
			validateProperties();
			validateDisplayList();
		}
		
		//-----------------------------------------------------------------
		//  backgroup
		//-----------------------------------------------------------------
		private function drawBackgrounp():void
		{
			drawer.clear();
			if(img.border)
				drawer.lineStyle(1,getStyle("borderColor") as uint,1,false,LineScaleMode.NONE);
			drawer.beginFill(img.backgroundColor,img.backgroundAlpha);
			drawer.drawRect(0,0,img.width,img.height);
		}
	}
}
import flash.display.Sprite;
import flash.text.TextField;

class Progresser extends Sprite
{
	public function Progresser()
	{
		initTxt();
	}
	//--------------------------------------------------------------------------
	//
	//  variables
	//
	//--------------------------------------------------------------------------
	private var _value:Number;
	private var txt:TextField;
	//--------------------------------------------------------------------------
	//
	//  Properties
	//
	//--------------------------------------------------------------------------
	/**
	 * 进度值 
	 */
	public function get value():Number
	{
		return _value;
	}
	
	/**
	 * @private
	 */
	public function set value(value:Number):void
	{
		_value = value;
		draw();
	}
	//--------------------------------------------------------------------------
	//
	//  Method
	//
	//--------------------------------------------------------------------------
	//-----------------------------------------------------------------
	//  txt
	//-----------------------------------------------------------------
	private function initTxt():void
	{
		txt = new TextField();
		addChild(txt);
	}
	
	private function draw():void
	{
		txt.text = "LOADING..."+int(value)+" %";
		trace(txt.text)
	}
}